import plugin from '../../../lib/plugins/plugin.js'
import fs from "fs";
import lodash from "lodash";
import { Config } from '../components/index.js'
import { YamlReader , puppeteer , common} from '../model/index.js';

const configs = {
"花糕" : "huagao",
"地图" : "map",
"终端" : "cmd",
"戳一戳" : "zhenxunchuo",
"真寻休息" : "sleep",
"话痨统计" : "carp",
"谁艾特我" : "whoatme",
"伪造消息" : "remor",
"上传面板图" : "panelbg",
"我的新昵称" : "callme",
"插件管理器" : "manager",
"文本回复一" : "txtone",
"文本回复二" : "txttwo",
"原神注册时间" : "genshintime"
}

let managereg = new RegExp(`^#?插件设置(${Object.keys(configs).join("|")})(开启|关闭|\\d+)$`)
export class NewConfig extends plugin {
    constructor() {
        super({
            name: '设置',
            event: 'message',
            priority: 100,
            rule: [
                {
                    reg: managereg,
                    fnc: 'Config_manage',
                    permission: 'master'
                },
                 {
                    reg: '^#插件设置全部(开启|关闭)$',
                    fnc: 'all',
                    permission: 'master'
                },
                {
                    reg: '^#插件设置$',
                    fnc: 'set',
                    permission: 'master'
                }
            ]
        })
    }
    
    // 更改配置
    async Config_manage(e) {
        // 解析消息
        let regRet = managereg.exec(e.msg)
        let index = regRet[1]
        let yes = regRet[2] == "开启" ? true : false
        Config.modify("admin_set", configs[index], yes)
        setTimeout(() => { this.set(e) }, 50)
        //延迟50毫秒防止html模板未刷新
        return true
    }

   async all(e) {
      let yes = e.msg.includes("全部开启") ? true : false
      let arr = Object.keys(configs)
         for (let i = 0; i < arr.length; i++) {
         Config.modify("admin_set", configs[arr[i]], yes)
         }
      setTimeout(() => { this.set(e) }, 50);
      //延迟50毫秒防止html模板未刷新
      return true
   }

    //渲染发送图片
    async set(e) {
        let config = await Config.admin_set
        let data = {
            huagao: getStatus(config.huagao),
            map: getStatus(config.map),
            cmd: getStatus(config.cmd),
            zhenxunchuo: getStatus(config.zhenxunchuo),
            sleep: getStatus(config.sleep),
            carp: getStatus(config.carp),
            whoatme: getStatus(config.whoatme),
            remor: getStatus(config.remor),
            panelbg: getStatus(config.panelbg),
            callme: getStatus(config.callme),
            manager: getStatus(config.manager),
            txtone: getStatus(config.txtone),
            txttwo: getStatus(config.txttwo),
            genshintime: getStatus(config.genshintime),
            bg: await rodom(), //获取底图
        }
        //渲染图像
        return await puppeteer.render("admin/index", {
            ...data,
        }, {
            e,
            scale: 2.0
        });
    }
}

//随机底图
const rodom = async function () {
    var image = fs.readdirSync(`./plugins/js-box-plugin/resources/admin/imgs/bg`);
    var list_img = [];
    for (let val of image) {
        list_img.push(val)
    }
    var imgs = list_img.length == 1 ? list_img[0] : list_img[lodash.random(0, list_img.length - 1)];
    return imgs;
}

const getStatus = function (rote) {
    if (rote) {
        return `<div class="cfg-status" >已开启</div>`;
    } else {
        return `<div class="cfg-status status-off">已关闭</div>`;
    }

}

